package com.danger.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private static String url = "http://www.imooc.com/api/teacher?type=4&num=30";
    private List<NewsBean> list = new ArrayList<NewsBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.lv_main);
        new NewsAsyncTask().execute(url);

    }

    class NewsAsyncTask extends AsyncTask<String, Void, List<NewsBean>> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected List<NewsBean> doInBackground(String... params) {
            return getJsonData(params[0]); // params[0]表示请求的网址
        }

        @Override
        protected void onPostExecute(List<NewsBean> result) {
            super.onPostExecute(result);
            NewsAdapter adapter = new NewsAdapter(list, MainActivity.this,listView);
            listView.setAdapter(adapter);
        }
    }

    private List<NewsBean> getJsonData(String url) {
        try {
            String json = readStream(new URL(url).openStream());
            JSONObject object = new JSONObject(json);
            int status = object.getInt("status");
            if (status == 1) {
                JSONArray list_news = object.getJSONArray("data");
                for (int i = 0; i < list_news.length(); i++) {
                    NewsBean bean = new NewsBean();
                    JSONObject beanObject = list_news.getJSONObject(i);
                    bean.setNewsTitle(beanObject.getString("name"));
                    bean.setNewsIconUrl(beanObject.getString("picSmall"));
                    bean.setNewsContent(beanObject.getString("description"));
                    list.add(bean);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String readStream(InputStream is) {
        InputStreamReader reader;
        String result = "";
        try {
            reader = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(reader);
            String line = "";
            while ((line = br.readLine()) != null)
                result = result + line;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
